package org.adg.code.controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.adg.code.model.DownloadFile;
import org.adg.code.model.NewReportReturn;
import org.adg.code.model.ReportValues;
import org.adg.code.model.SaveReportValues;
import org.adg.code.service.ReportManager;
import org.adg.code.service.ReportManagerImpl;



public class ReportController {

	/*This method return the resultset containing report catagory names*/
	public ResultSet getReportCatNames(HttpServletRequest request, HttpServletResponse response)
	{
		
		ReportManager um=new ReportManagerImpl();
		
		ResultSet rs=um.getReportCatNames();
		return rs;
		
	}
	
	/*Returns an object of NewReportReturn type which contains the list of Reportparticular type name ,id and uitypes*/
	public NewReportReturn getReportSet(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		String uname=(String)session.getAttribute("uname");
		ReportManager rm=new ReportManagerImpl(); 
		String rname=request.getParameter("rcatname");
		return rm.getReportSet(rname,uname);
	}
		
	/*This method gets the report values from the jsp page and save it to the database */
	public boolean saveReport(HttpServletRequest request, HttpServletResponse response)
	{	
		String nor=(String)request.getParameter("numoffields");
		int numofvalues=Integer.parseInt(nor);
		SaveReportValues[] srv=new SaveReportValues[numofvalues+1];
		srv[0]=new SaveReportValues();
		srv[0].setrepCat(request.getParameter("rcatname"));
		srv[0].setrepName(request.getParameter("rname"));
		srv[0].setuname(request.getParameter("ruser"));
		srv[0].setref(request.getParameter("ref"));
		
		
		for(int i=1;i<=numofvalues;i++)
		{
			srv[i]=new SaveReportValues();
			srv[i].setrptid(Integer.parseInt(request.getParameter("label"+i)));
			srv[i].setvalue(request.getParameter("value"+i));
		
			
		}
		ReportManager rm=new ReportManagerImpl(); 
		return rm.saveReport(srv,numofvalues);
		
	}
	
	/*returns the object of NewReportReturn type which contains the list of report particular type name,id and uitypes*/
	public NewReportReturn openReportSet(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		String uname=(String)session.getAttribute("uname");
		String rname=(String)request.getParameter("catgname");
		ReportManager rm=new ReportManagerImpl(); 
		return rm.getReportSet(rname,uname);
		
		
	}
	
	/*returns the object of ReportValues type which contains the list report values*/
	public ReportValues openReportValues(HttpServletRequest request, HttpServletResponse response)
	{
		ReportManager rm=new ReportManagerImpl(); 
		String rname=(String)request.getParameter("name");
		return rm.getReportValues(rname);
		
	}
	
	/*Gets the modified report values and updates the database*/
	public boolean updateReport(HttpServletRequest request, HttpServletResponse response)
	{
		String nor=(String)request.getParameter("numoffields");
		int numofvalues=Integer.parseInt(nor);
		ReportValues[] rv=new ReportValues[numofvalues+1];
		rv[0]=new ReportValues();
		rv[0].setReportcat((request.getParameter("rcatname")));
		rv[0].setReportname((request.getParameter("rname")));
		rv[0].setUsername((request.getParameter("ruser")));
		rv[0].setRef((request.getParameter("ref")));
		
		
		for(int i=1;i<=numofvalues;i++)
		{
			rv[i]=new ReportValues();
			rv[i].setRptid((Integer.parseInt(request.getParameter("label"+i))));
			rv[i].setValues((request.getParameter("value"+i)));
		
			
		}
		ReportManager rm=new ReportManagerImpl(); 
		return rm.updateReport(rv,numofvalues);
		
		
		
	}
	
	/*Returns a list of report names which are under the selected report category and belongs to the users under the logged in user*/
	public ArrayList<String> getReportNames(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		String uname=(String)session.getAttribute("uname");
		String rname=(String)request.getParameter("rname");
		ReportManager rm=new ReportManagerImpl(); 
		
		return rm.getReportNames(rname,uname);
	}
	
	/*Deletes the selected report*/
	public void removeReport(HttpServletRequest request, HttpServletResponse response)
	{
		ReportManager rm=new ReportManagerImpl();
		
		if(rm.removeReport(request.getParameter("reportname")));
		{	 
			try {
				response.sendRedirect("adg.do?method=openreport");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/*Gets the modified report values updates the database and then Generates the report*/
	public boolean generateReport(HttpServletRequest request, HttpServletResponse response)
	{
		String nor=(String)request.getParameter("numoffields");
		int numofvalues=Integer.parseInt(nor);
		ReportValues[] rv=new ReportValues[numofvalues+1];
		rv[0]=new ReportValues();
		rv[0].setReportcat((request.getParameter("rcatname")));
		rv[0].setReportname((request.getParameter("rname")));
		rv[0].setUsername((request.getParameter("ruser")));
		rv[0].setRef((request.getParameter("ref")));
		
		
		for(int i=1;i<=numofvalues;i++)
		{
			rv[i]=new ReportValues();
			rv[i].setRptid((Integer.parseInt(request.getParameter("label"+i))));
			rv[i].setValues((request.getParameter("value"+i)));
		
			
		}
		ReportManager rm=new ReportManagerImpl(); 
		if(rm.updateReport(rv,numofvalues))
		{
			if(rm.generateReport(rv,numofvalues))
			{
				return true;
			}
			return false;
		}
		return false;
	}
	
	/*Returns a list of Generated report names which are under the selected report category */
	 /* and belongs to the users under the logged in user*/
	public DownloadFile getDownloadFile(String rname)
	{
		
		
		ReportManager rm=new ReportManagerImpl(); 
		
		return rm.getDownloadFile(rname);
	}
	
	public boolean repNameExists(String rname)
	{
		ReportManager rm=new ReportManagerImpl(); 
		return rm.repNameExists(rname);
	}
}
